package com.sywb.chuangyebao.contract;

import android.app.Activity;
import android.widget.ImageView;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.GoodAtBean;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public interface EmpryContract {

    interface View extends BaseRecyclerContract.View {

        void userAdd(LinkedHashMap<Integer, String> addMap);

        void userDel(LinkedHashMap<Integer, String> delMap);

    }

    class Presenter extends BaseRecyclerContract.Presenter<View> {

        public AskAdapter askAdapter;
        public List<User> userList = new ArrayList<>();

        LinkedHashMap<Integer, String> addMap = new LinkedHashMap<>();
        LinkedHashMap<Integer, String> delMap = new LinkedHashMap<>();

        @Override
        public void onStart() {
            super.onStart();
            askAdapter = new AskAdapter(mActivity);
            setRecyclerAdapter(askAdapter, false);

            initData();
        }

        @Override
        public void onRecyclerItemClick(int postion) {
            super.onRecyclerItemClick(postion);

            User user = askAdapter.getItem(postion);

            String at = "@" + user.display_name + " ";

            if (user.isSelected) {
                addMap.remove(user.user_id);
                delMap.put(user.user_id, at);
            } else {
                addMap.put(user.user_id, at);
                delMap.remove(user.user_id);
            }
            user.isSelected = !user.isSelected;
            askAdapter.notifyDataSetChanged();

//                    if (askList.containsKey(user.display_name)) {
//                        askList.remove(user.display_name);
//                    } else if (askList.size() == 5) {
//                        ToastUtils.show(mActivity, "sorry,最多只能选5个");
//                        return;
//                    } else {
//                        askList.put(user.display_name, user.user_id);
//                    }
//                    askAdapter.notifyDataSetChanged();
//                    mView.setAskConfirmText(askList);
        }

        public void getUserChange() {
            mView.userDel(delMap);
            mView.userAdd(addMap);
            delMap.clear();
            addMap.clear();
        }

        public void initData() {

            for (int i = 0; i < 20; i++) {
                String display_name = "名称" + i;
                String signature = "我说是第" + i + "号人物简介";

                userList.add(new User(i, display_name, signature));
            }

            askAdapter.setDatas(userList);
        }

    }

    class AskAdapter extends BaseRecyclerAdapter<User> {

        AskAdapter(Activity activity) {
            super(activity, R.layout.item_questions_to_people);
        }

        @Override
        protected void setItemData(ViewHolderHelper viewHolderHelper, int position, User model) {
            viewHolderHelper.setImageResource(R.id.iv_tutor_chose, model.isSelected ? R.drawable.chose_fill : R.drawable.chose_normal);
            ImageView imageView = viewHolderHelper.getView(R.id.iv_tutor_face);
            imageView.setImageResource(R.drawable.head_image);
            // GlideUtils.getCircleImageToUrl(mActivity, model.avatar, imageView);
            viewHolderHelper.setText(R.id.tv_tutor_name, model.display_name);
            viewHolderHelper.setText(R.id.tv_tutor_other, model.signature);
        }
    }

    class User {

        User(int user_id, String display_name, String signature) {
            this.user_id = user_id;
            this.display_name = display_name;
            this.signature = signature;
            isSelected = false;
        }

        public String display_name;
        public String avatar;
        public Integer user_group_id;
        public int user_id;
        public int fans_num;
        public int answer_num;
        public String signature;
        public List<GoodAtBean> good_at;

        public boolean isSelected;

        public String getGoodAtString() {
            StringBuilder builder = new StringBuilder();
            if (good_at != null && good_at.size() > 0) {
                for (GoodAtBean goodAtBean : good_at) {
                    builder.append(goodAtBean.name).append("、");
                }
                return builder.toString().substring(0, builder.length() - 1);
            } else {
                return builder.toString();
            }
        }

    }
}

